<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Live extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('Lives');
		$this->load->model('courses');
		$this->load->model('videos');
	}

	/**
	** 网课-视频列表
	*/
	public function index($type = 1,$page = 1){
		

		//直播中
		if(!in_array($type, array(1,2,3))){
			show_message($_SERVER['HTTP_REFERER'], "异常操作");exit;
		}
		if($type == 1){
			$starting = $this->Lives->get_live_starting();
			$this->smarty->assign("list", $starting);
		}else if($type == 2){
			//即将直播
			$waitting = $this->Lives->get_jijiangzhibo_list();
			$this->smarty->assign("list", $waitting);
		}else if($type == 3){
			//历史直播
			$lishi_list = $this->Lives->get_lishi_gzhibo_list();
			$this->smarty->assign("list", $lishi_list);
		}
		$this->smarty->assign("type", $type);
		$this->smarty->display("course/live_list.html");
	}


	public function detail($id, $page = 0)
  	{
  		$info = $this->videos->get_course_info($id);
  		$school_course_count = $this->videos->get_school_course_count($info['sid']);
		$xuesheng_count = $this->videos->get_school_xuesheng_count($info['sid']);
		$info['course_count'] = $school_course_count;
		$info['xuesheng_count'] = $xuesheng_count;
		//判断直播是否结束；
		$live_status = $this->Lives->get_live_status($id);
		if(empty($live_status)){
			$live_status = 1;
		}else{
			$live_status = 0;
		}
		if($this->user_data['id'] > 0){
			$check = $this->Lives->check_collect_status($this->user_data['id'],$id,2);
			if($check){
				$info['is_collect'] = 1;
			}else{
				$info['is_collect'] = 0;
			}
		}else{
			$info['is_collect'] = 0;
		}

		/*$order_buy = $this->courses->check_is_buy($id,2);
		if(!empty($order_buy)){
			$effective_time = $order_buy['add_time'] + $order_buy['effective_time']*24*60*60;
			$time = time();
			if($time < $effective_time){
				$is_buy = 1;
			}else{
				$is_buy = 2;
			}
		}else{
			$is_buy = 0;
		}*/
		$is_buy = 0;
		if($this->user_data['id']){
			$order_buy = check_effective_time($this->user_data['id'], $id, 2);
			if($order_buy || ( isset($info['is_free']) && $info['is_free'] == 1 && get_vip_endtime($this->user_data['id']) > time())){
				$is_buy = 1;
			}
		}
		$limit = 0;
		$offset = 1;
		if($page > 0){
			$limit = ($page - 1) * $offset;
		}
		$comment_list = $this->videos->get_comment_list($id,0,$offset,$limit);
		
		$this->smarty->assign("info", $info);
		$this->smarty->assign("comment_list", $comment_list);
		$this->smarty->assign("is_buy", $is_buy);
		$this->smarty->assign("live_status", $live_status);
		$this->smarty->assign("menutag", "course");
		$this->smarty->display("course/live_detail.html");
  	}
}
